var UserTutorTrade = app.controller('UserTutorTrade', function($http, $scope, $rootScope, $location,$cordovaDevice,$cordovaChat,UserTutorTradeData) {
	try{
		$rootScope.showf=false;
		$scope.name='';
		$scope.list1={};
		$scope.list2={};
		$scope.s='';
		var s='';
		if($rootScope.btrs1_trade!=undefined&&$rootScope.btrs1_trade!=""&&$rootScope.btrs1_trade!=null){
			s=$rootScope.btrs1_trade;
		}
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=trade1"+"&t="+s;
		$http.jsonp(url).success(function(data) {
			$scope.list1=data.retval;
			for ( var i = 0; i < $scope.list1.length; i++) {
				if($scope.list1[i].h>0){
					var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=trade2"+"&id="+$scope.list1[i].trade_id;
					$http.jsonp(url).success(function(data) {
						$scope.list2=data.retval;
						
						if($rootScope.btrs1_trade!=undefined&&$rootScope.btrs1_trade!=""&&$rootScope.btrs1_trade!=null){
							$scope.name=$rootScope.btrs1_trade;
							for ( var j = 0; j < $scope.list2.length; j++) {
								if($scope.name==$scope.list2[j].trade_name){
									$scope.list2[j].h=1;
									break;
								}
							}
						}else if($scope.list2.length>0){
							$scope.list2[0].h=1;
						}
					});
				}
			}
		});
		
		$scope.keyPress=function(e){
			if(e.which==13||e.which==9){
				$scope.search();
			}
		}
		
		//搜索
		$scope.search=function(){
			$("#search").blur();
			if ($scope.busy) {
				return false;
			}
			$scope.busy = true;
			var id=0;
			for ( var i = 0; i < $scope.list1.length; i++) {
				if($scope.list1[i].h>0){
					id=$scope.list1[i].trade_id;
					break;
				}
			}
			var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=trade2"+"&id="+id+"&s="+$scope.s;
			$http.jsonp(url).success(function(data) {
				$scope.list2=data.retval;
				$scope.busy = false;
			});
		}
		
		//一级分类点击
		$scope.click1=function(s){
			var id=0;
			for ( var i = 0; i < $scope.list1.length; i++) {
				if(i==s.$index){
					$scope.list1[i].h=1;
					id=$scope.list1[i].trade_id;
				}else{
					$scope.list1[i].h=0;
				}
			}
			var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=trade2"+"&id="+id;
			$http.jsonp(url).success(function(data) {
				$scope.list2=data.retval;
			});
		}
		//二级分类点击
		$scope.click2=function(s){
			$rootScope.btrs1_trade=$scope.list2[s.$index].trade_name;
			$rootScope.back();
		}
		
	}
	catch (e) {
		showcatch(e);
	}
});
UserTutorTrade.UserTutorTradeData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}